<?php
/**
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_1
 * @license    GNU/GPL
*/
 
// no direct access
// ajouter le tank dans la base de donner des replays 
 
defined( '_JEXEC' ) or die( 'Restricted access' );
 
jimport( 'joomla.application.component.view');
jimport( 'joomla.filesystem.path' );

jimport( 'joomla.database.database' );
 
/**
 * HTML View class for the HelloWorld Component
 *
 * @package    HelloWorld
 */
 
class wotclanViewreplay extends JView
{
    function display($tpl = null)
    {
    	include JPATH_COMPONENT.DS.'functions.php';
    	$cuser =& JFactory::getUser();
    	$cusr_id = $cuser->get('id');
      $db =& JFactory::getDbo();
      $greeting = "Les replays!";
      $this->assignRef( 'greeting', $greeting );
      parent::display($tpl);
      $query = $db->getQuery(true);

      $query="SELECT * FROM `#__wotclan_replays`  ORDER BY datebattle DESC LIMIT 0,100";
     //echo $query;
      $db->setQuery($query);
      $db->query();
      $num_rows = $db->getNumRows();
      $rows = $db->loadObjectList();
      echo "<div align='center'>";
      
      echo "<table><tr><td width='250' align='center' style='border:1px solid white'>Joueur</td><td width='250' align='center' style='border:1px solid white'>Vehicule</td><td width='250' align='center' style='border:1px solid white'>Date</td><td width='250' align='center' style='border:1px solid white'>Titre</td><td width='250' align='center' style='border:1px solid white'></td></tr>";
 
      foreach ( $rows as $row ) {
      	
      	$user =& JFactory::getUser($row->uid);

    		echo "<tr><td width='250' align='center' style='border:1px solid white'><a href='/index.php/forum/profil/".$user->id."-".$user->username."'>".$user->username. 
                "</a></td>";
    		// TODO
    		// if ( user = current user permettre d effacer
    		
    		// TODO
    		// chercher la battle $row->battle
            $query2 = $db->getQuery(true);
            $query2="SELECT * FROM `#__wotclan_battle` WHERE uniqid='".$row->battle."' ";
            
            // echo $query;
            $db->setQuery($query2);
            $db->query();
            $num_rows2 = $db->getNumRows();
            $rows2 = $db->loadObjectList();
            if ($num_rows2==1){
                $query3 = $db->getQuery(true);
                $query3="SELECT * FROM `#__wotclan_tanks` WHERE uniqid='".$rows2[0]->tank."' ";
                $db->setQuery($query3);
                $db->query();
                $num_rows3 = $db->getNumRows();
                $rows3 = $db->loadObjectList();
                if ($num_rows3==1){
                    $tank=$rows3[0]->name;
                    $cname=$rows3[0]->common_name;
                    $ttype=$rows3[0]->tanktype;
		    $uniqid=$rows3[0]->uniqid;
                } else {
                    
                    echo "Prob avec le tank ". $rows2[0]->tank ;
                }// else error


            } // else error
        echo "<td width='250' align='center' style='border:1px solid white'><a href=''>"; // ajouter le show du tank
        showimgtank($tank,$cname,$ttype,$uniqid);
        echo " </a></td><td width='250' align='center' style='border:1px solid white'> ".preg_replace("/(\d{4}\-\d{2}\-\d{2}).*/","$1",$row->datebattle)."</td><td width='250' align='center' style='border:1px solid white'> ".$row->title."</td><td width='250' align='center' style='border:1px solid white'> <a href='/media/wotclan/replays/".$row->file."'>Download</a> ";
        //echo "<h1>uid=".$row->uid." user = $user	</h1>";
        if ($row->uid == $cusr_id){
        	echo "<a href='?view=delete&type=replay&id=".$row->uniqid."'>Delete</a>";
        }
        echo "</td></tr>"; // <a href='?view=delete'>delete</a><br>;
		    
		}	
		echo "</table></div>";
    
    }
}



